package com.fzy.common.random;

import java.text.NumberFormat;

public class SequenceIdGenUtil {
    private SequenceIdGenUtil() {
    }

    public static SequenceIdGenUtil sequenceIdGen = null;

    /**
     * 4位顺序号	1-9999
     */
    private static Integer seqNo4 = 0;

    /**
     * 10位顺序号	1-9999999999
     */
    private static Long seqNo10 = 0L;
    private static Boolean isInit = false;

    public static SequenceIdGenUtil getInstance() {
        //未初始化
        if (!isInit) {
            synchronized (isInit) {
                if (!isInit) {
                    sequenceIdGen = new SequenceIdGenUtil();
                    //初始化成功
                    isInit = true;
                }
            }
        }
        return sequenceIdGen;
    }

    /**
     * 生成4位顺序号 不补0
     *
     * @return
     */
    public static int getGen() {
        return SequenceIdGenUtil.getInstance().genSeqNo4();
    }

    /**
     * 生成4位顺序号 补0
     *
     * @return
     */
    public static String getGen4() {
        return SequenceIdGenUtil.getInstance().genSeqNo04();
    }

    /**
     * 生成4位顺序号 补0
     *
     * @return
     */
    public static String getGen04() {
        //方法一：String.format("%04d", n)

        //java jdk中补0的方法二
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);

        int n = SequenceIdGenUtil.getInstance().genSeqNo4();

        String s = formatter.format(n);

        return s;
    }

    /**
     * 生成10位顺序号 不补0
     *
     * @return
     */
    public static long genSeq10() {
        return SequenceIdGenUtil.getInstance().genSeqNo10();
    }

    /**
     * 生成4位顺序号
     *
     * @return
     */
    public int genSeqNo4() {
        synchronized (seqNo4) {
            if (seqNo4 != null && seqNo4 >= 9999) {
                seqNo4 = 1;
            } else {
                seqNo4++;
            }
            return seqNo4;
        }
    }

    /**
     * 生成4位顺序号 不够4位补0
     *
     * @return
     */
    public String genSeqNo04() {
        String v = "";
        synchronized (seqNo4) {
            if (seqNo4 != null && seqNo4 >= 9999) {
                v = "000";
                seqNo4 = 1;
            } else {

                seqNo4++;

                int k = 4 - String.valueOf(seqNo4).length();

                for (; k > 0; k--) {
                    v += "0";
                }
            }
            return v + seqNo4;
        }
    }

    /**
     * 生成10位顺序号
     *
     * @return
     */
    public long genSeqNo10() {
        synchronized (seqNo10) {
            if (seqNo10 != null && seqNo10 >= 9999999999L) {
                seqNo10 = 1L;
            } else {
                seqNo10++;
            }
            return seqNo10;
        }
    }

    public static void main(String[] args) {

        for (int i = 0; i < 10000; i++) {
            int j = SequenceIdGenUtil.getGen();

            System.out.println(String.format("%04d", j));
        }

    }
}
